//
//  BAKit_SorterSelectedView.m
//  BASorterSelectedView
//
//  Created by Mr_zhang on 2017/9/13.
//  Copyright © 2017年 Mr_zhang. All rights reserved.
//

#import "BAKit_SorterSelectedView.h"
#import "BASorterSelectedView.h"
#import "BAKit_WholeView.h"
#import "BAKit_SortView.h"
#import "BAKit_ScreeningView.h"

#import <Masonry.h>

@interface BAKit_SorterSelectedView()
/** 全部 */
@property (nonatomic, strong) UIButton *wholeBtn;
/** 排序 */
@property (nonatomic, strong) UIButton *sortBtn;
/** 筛选 */
@property (nonatomic, strong) UIButton *screeningBtn;
/** 保存最后一个选中的按钮 */
@property (nonatomic, strong) UIButton *selectedBtn;

@property (nonatomic, strong) BAKit_WholeView *wholeView;

@property (nonatomic, strong) BAKit_SortView *sortView;

@property (nonatomic, strong) BAKit_ScreeningView *screeningView;

@end

@implementation BAKit_SorterSelectedView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUpSubView];
    }
    return self;
}

- (void)setUpSubView
{
    /** 全部 */
    CGRect wholeFrame = CGRectMake(0, 0, BAKit_SCREEN_WIDTH/3, self.frame.size.height);
    self.wholeBtn = [self createBtnWithFrame:wholeFrame withTitle:@"全部" SelectedBtnType:BAKit_SelectedViewBtnWholeType fontSize:13];
    [self addSubview:_wholeBtn];
    
    /** 排序 */
    CGRect sortFrame = CGRectMake(BAKit_SCREEN_WIDTH/3, 0, BAKit_SCREEN_WIDTH/3, self.frame.size.height);
    self.sortBtn = [self createBtnWithFrame:sortFrame withTitle:@"排序" SelectedBtnType:BAKit_SelectedViewBtnSortType fontSize:13];
    [self addSubview:_sortBtn];
    
    
    /** 筛选 */
    CGRect screeningFrame = CGRectMake(BAKit_SCREEN_WIDTH/3 *2, 0, BAKit_SCREEN_WIDTH/3, self.frame.size.height);
    self.screeningBtn = [self createBtnWithFrame:screeningFrame withTitle:@"筛选" SelectedBtnType:BAKit_SelectedViewBtnScreeningType fontSize:13];
    [self addSubview:_screeningBtn];
}

- (UIButton *)createBtnWithFrame:(CGRect)frame  withTitle:(NSString *)title SelectedBtnType:(SelectedViewBtnType)SelectedBtnType fontSize:(CGFloat)fontsize
{
    UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    customBtn.frame = frame;
    [customBtn setTitle:title forState:UIControlStateNormal];
//    [customBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [customBtn setTitleColor:BAKit_Color_Gray_1_pod  forState:UIControlStateNormal];
    [customBtn setTitleColor:BAKit_Color_Red_pod forState:UIControlStateSelected];
    customBtn.layer.cornerRadius = 3;
    customBtn.tag = SelectedBtnType;
    customBtn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
    [customBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [customBtn setImage:[UIImage imageNamed:@"updown_noamal"] forState:UIControlStateNormal];
    [customBtn setImage:[UIImage imageNamed:@"updown_selected"] forState:UIControlStateSelected];
    
    [customBtn setImageEdgeInsets:UIEdgeInsetsMake(0, frame.size.width - 25, 0, 10)];
    [customBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 25)];
    return customBtn;
}

- (void)clickMethod:(UIButton *)sender
{
    if (!(sender.tag == self.selectedBtn.tag))
        self.selectedBtn.selected = NO;
    sender.selected = !sender.selected;
    self.selectedBtn = sender;
    
    if (sender.tag == BAKit_SelectedViewBtnWholeType)
    {
        [self.sortView removeFromSuperview];
        [self.screeningView removeFromSuperview];
        
        if (sender.selected == YES)
        {
             self.wholeView = [[BAKit_WholeView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame), BAKit_SCREEN_WIDTH, 300)];
            [[self viewController].view addSubview:self.wholeView];
        }
        else
            [self.wholeView removeFromSuperview];
    }
    else if (sender.tag == BAKit_SelectedViewBtnSortType)
    {
        [self.wholeView removeFromSuperview];
        [self.screeningView removeFromSuperview];
        
        if (sender.selected == YES)
        {
            self.sortView = [[BAKit_SortView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame), BAKit_SCREEN_WIDTH, 300)];
            [[self viewController].view addSubview:self.sortView];
        }
        else
            [self.sortView removeFromSuperview];
    }
    else if (sender.tag == BAKit_SelectedViewBtnScreeningType)
    {
        [self.wholeView removeFromSuperview];
        [self.sortView removeFromSuperview];
        
        if (sender.selected == YES)
        {
            self.screeningView = [[BAKit_ScreeningView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame), BAKit_SCREEN_WIDTH,BAKit_SCREEN_HEIGHT - 64 - self.frame.size.height)];
            [[self viewController].view addSubview:self.screeningView];
        }
        else
            [self.screeningView removeFromSuperview];
    }
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.wholeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@0);
        make.width.equalTo(@(BAKit_SCREEN_WIDTH/3));
        make.height.equalTo(@(self.frame.size.height));
    }];
    
    [self.sortBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.wholeBtn.mas_right).with.offset(0);
        make.top.equalTo(@0);
        make.width.equalTo(@(BAKit_SCREEN_WIDTH/3));
        make.height.equalTo(@(self.frame.size.height));
    }];
    
    [self.screeningBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sortBtn.mas_right).with.offset(0);
        make.top.equalTo(@0);
        make.width.equalTo(@(BAKit_SCREEN_WIDTH/3));
        make.height.equalTo(@(self.frame.size.height));
    }];
}

- (UIViewController *)viewController
{
    for (UIView *next = [self superview]; next; next = next.superview){
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
            return (UIViewController *)nextResponder;
    }
    return nil;
}

@end
